<?php

/* 
 * Class teia\webdev\php\adt\Page
 * Core class for creating web pages dynamically
 * 
 * @author Bernardo Quibiana
 * @date created: 20111001
 * 
 * @date modified: 20111002
 * @modified: Bernardo Quibiana
 */

// namespace declaration: Abstract Data Types
namespace teia\webdev\php\adt;

use teia\webdev\php\util\ArrayUtil;

require_once './../util/ArrayUtil.inc';

class Page {

	protected $id;	// [String] unique page identifier
	protected $title;	// [Strign] the page title
	protected $body;	// [String] the body html content
	protected $intStyle;	// [String] internal style sheet
	protected $extStyle;	// [String] path to external stylesheet
	protected $keywords;	// [String[]] array with keywords for web search [Semantic Web]

	protected $strict;	// [boolean] specify if the creation should be strict
	
	static function createTag($element, $content, $attributes = NULL) {
		$attribStr = '';
		if ($attributes) {
			
		}
		
		return "<" . $element . $attribStr . ">" . $content . "</" . $element . ">";
	}
	
	static function displayTag($element, $content, $attributes = NULL) {
		echo self::createTag($element, $content, $attributes = NULL);
	}
	
	public function __construct($id, $title, $body, $extStyle = NULL, $intStyle = NULL, $keywords = NULL) {
		$this->id = $id;
		$this->title = $title;
		$this->body = $body;
			
		// parse
		if ($extStyle) {
				
		}
			
		if ($intStyle) {
				
		}
			
		if ($keywords && is_array($keywords)) {
			if(is_array($keywords)) {
				$this->keywords = $keywords;
			} else {
				array_push($this->keywords, $keywords);
			}
		}
	}	// end of __construct

	public function __get($name) {
		return $this->$name;
	}
	
	public function __set($name, $value) {
		$this->$name = $value;
	}

	public function createTitleTag() {
		return self::createTag("title", $this->title);
	}
	
	public function displayTitle() {
		echo $this->createTitleTag();
	}

	public function createIntStyleTag() {
		return self::createTag("style", $this->intStyle);
	}
	
	public function displayIntStyle() {
		echo $this->createIntStyleTag();
	}

	public function createExtStyleTag() {
		return "<link rel=\"stylesheet\" href=\"" . $this->extStyle . "\" />";
	}
	
	public function displayExtStyle() {
		echo $this->createExtStyleTag();
	}

	public function createKeywordsTag() {
		return "<meta name=\"keywords\" content=\"" . ArrayUtil::assocArrayToStr($this->keywords, ", ") . "\" />";
	}
	
	public function displayKeywords() {
		echo $this->createKeywordsTag();
	}

	public function createHeadTag() {
		$title = $this->createTitleTag();
		$extStyle = $this->createExtStyleTag();
		$intStyle = $this->createIntStyleTag();
		$keywords = $this->createKeywordsTag();
			
		return self::createTag("head", "\n" . $title . "\n" . $extStyle . "\n" . $intStyle . "\n" . $keywords . "\n");
	}
	
	public function displayHead() {
		echo $this->createHeadTag();
	}

	public function createBodyTag() {
		return self::createTag("body", "\n" . $this->body . "\n");
	}
	
	public function displayBody() {
		echo $this->createBodyTag();
	}

	public function createDocument() {
		$head = $this->createHeadTag();
		$body = $this->createBodyTag();
		$html = self::createTag("html", "\n" . $head . "\n" . $body . "\n");
			
		return "<!DOCTYPE HTML>\n" . $html;
	}
	
	public function display() {
		echo $this->createDocument();
	}

	public function addContent($content) {
		$this->body .= $content;
	}

	public function addKeyword($keyword) {
		array_push($this->keywords, $keyword);
	}

	public function searchKeywords($target) {
			
	}

	public function searchContent($target) {
			
	}

	public function search($target) {
			
	}
	
	public function saveToFile($path, $append = FALSE) {
		$rc = 0;
		$mode = $append ? 'a' : 'w';	// append or write
		$handle = fopen($path, $mode);
		$doc = $this->createDocument();	// generate the html document
		
		if ($handle) {
			flock($handle, LOCK_EX);	// lock file for writing
			$rc = fwrite($handle, $doc);	// write data to file
			flock($handle, LOCK_UN);	// unlock file
			fclose($handle);	// close file
		}
		
		return $rc;
	}
}	// end of class Page

?>